$server = 'http://localhost:8080/';

window.onload = function(){
	$id = GetQueryString('id');
	$.ajax({
		url: $server+'commodity/detail.do',
		data: {commodityId: $id},
		type: "POST",
		datatype: 'json',		
		crossDomain:true, //设置跨域为true
		xhrFields:{
			withCredentials: true // 携带跨域cookie
		},
		success: function(result){
			console.log(result);
			if (result.code == 200){
				$('#name')[0].innerHTML = result.data.name;
				$('#subname')[0].innerHTML = result.data.subtitle;
				$('#content')[0].innerHTML = result.data.detail;
				$('#body_name')[0].innerHTML = result.data.name;
				$('#body_commodity_no')[0].innerHTML = result.data.id;
				$('#body_category')[0].innerHTML = result.data.categoryId;
				$('#stock')[0].innerHTML = result.data.stock;
			}else{
				alert(result.msg);
			}
		},
		error: function(XMLHttpRequest, textStatus, errorThrown){
			alert(textStatus);
		}
	});
}

// 从地址栏获取参数
function GetQueryString(name)
{
     var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
     var r = decodeURI(window.location.search.substr(1)).match(reg);
     if(r!=null)return  unescape(r[2]); return null;
}

function addBuyCar(){
	$id = GetQueryString('id');
	$count = $('#num').val();
	$.ajax({
		url: $server+'cart/add.do',
		data: {commodityId: $id, count: $count},
		type: "POST",
		datatype: 'json',		
		crossDomain:true, //设置跨域为true
		xhrFields:{
			withCredentials: true // 携带跨域cookie
		},
		success: function(result){
			console.log(result);
			if (result.code == 200){
				alert('加入购物车成功,跳转到购物车');
				window.location.href = 'cart.html';
			}else{
				alert(result.msg);
			}
		},
		error: function(XMLHttpRequest, textStatus, errorThrown){
			alert(textStatus);
		}
	});
}